package controller.business.fundCheck;

import controller.business.fundEntry.InspectorFundList;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.business.BusinessInfoServices;
import services.business.InspectorFundServices;
import services.business.InspectorInfoServices;
import services.dictionary.FundSetServices;
import wt.entity.business.BusinessInfo;
import wt.entity.business.BusinessInfoExample;
import wt.entity.business.InspectorFund;
import wt.staticData.ShortData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/fundCheck")
public class FundCheckController extends BaseController {


    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    FundSetServices fundSetServices;

    @Autowired
    InspectorFundServices inspectorFundServices;


    @RequiresPermissions("business:fundCheck:default")
    @RequestMapping("index")
    public String index() {
        return "business/fundCheck/index";
    }


    @RequiresPermissions("business:fundCheck:default")
    @RequestMapping("editDlg")
    public String editDlg(String businessId, Map map) throws Exception {

        List<InspectorFund> inspectorFundList = inspectorFundServices.selectByBusinessId(businessId);
        inspectorFundServices.lazyLoadInspectorInfo(inspectorFundList);
        map.put("fundSetList", fundSetServices.selectByExample(null));
        map.put("inspectorFundList", inspectorFundList);
        map.put("businessId",businessId);
        map.put(StaticConstant.EDIT_URL, "business/fundCheck/update");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);

        return "business/fundCheck/editDlg";
    }

    @RequiresPermissions("business:fundCheck:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(InspectorFundList inspectorFundList,String businessId) throws Exception {

        CommonResult commonResult = CommonResult.newCommonResult();

        BusinessInfo businessInfo = businessInfoServices.selectByPrimaryKey(businessId);

        if (1!=businessInfo.getFundStatus()) {
            commonResult.addMessage("已审核");
            return commonResult.setError();
        }


        if (inspectorFundList.getInspectorFundList().size() != businessInfo.getInspectorCount().intValue()) {
            commonResult.addMessage("专家与业务分配专家不匹配");
            return commonResult.setError();
        }

        inspectorFundServices.updateByPrimaryKeySelective(inspectorFundList.getInspectorFundList(), businessId, getLoginAccount().getStaffInfo().getId(), getLoginAccount().getStaffInfo().getName());

        return commonResult.setSuccess();
    }


    @RequiresPermissions("business:fundCheck:default")
    @ResponseBody
    @RequestMapping("select")
    public List<BusinessInfo> select() throws Exception {
        BusinessInfoExample businessInfoExample = new BusinessInfoExample();
        businessInfoExample.setOrderByClause("id desc");
        businessInfoExample.createCriteria().andFundStatusEqualTo(ShortData.s1);
        return businessInfoServices.selectByExample(businessInfoExample);
    }

    @RequiresPermissions("business:fundCheck:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {
        CommonResult commonResult = new CommonResult();
        int count = select().size();
        if (count == 0) {
            return commonResult.setError();
        } else {
            Map map = new HashMap();
            map.put("info", count + "条业务经费未审核");
            map.put("url", "business/fundCheck/index");
            commonResult.setData(map);
            return commonResult.setSuccess();
        }
    }



}
